package com.tduck.cloud.datav.service.charts;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.google.common.collect.Lists;
import com.tduck.cloud.datav.constant.ChartsConstants;
import com.tduck.cloud.datav.request.ChartDataRequest;
import com.tduck.cloud.datav.service.BaseChartDataService;
import com.tduck.cloud.datav.vo.GroupQueryResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * 排名轮播表
 *
 * @author tduck
 */
@Component
@Slf4j
public class TableBarChartDataService extends BaseChartDataService {
    @Override
    public Map<String, Object> getChartData(ChartDataRequest request) {
        List<Map<String, Object>> resultMap = Lists.newArrayList();
        GroupQueryResult groupQueryResult = getGroupQueryDataList(request);
        for (String key : groupQueryResult.getGroupAggsMap().keySet()) {
            List<Object> valueList = groupQueryResult.getGroupAggsMap().get(key);
            List<String> groupDataList = groupQueryResult.getGroupDataList();
            for (int i = 0; i < valueList.size(); i++) {
                Object val = valueList.get(i);
                Map<String, Object> map = MapUtil.newHashMap();
                map.put("name", CollUtil.get(groupDataList, i));
                map.put("value", val);
                resultMap.add(map);
            }
            break;
        }
        return MapUtil.of(ChartsConstants.DATA, resultMap);
    }
}
